home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / Other Langs / python / Lib / test / test_grammar.py < prev    next >
Encoding:
Text File  |  1994-04-01  |  8.8 KB  |  424 lines  |  [TEXT/R*ch]

  1. # Python test set -- part 1, grammar.
  2. # This just tests whether the parser accepts them all.
  3.  
  4. from test_support import *
  5.  
  6. print '1. Parser'
  7.  
  8. print '1.1 Tokens'
  9.  
  10. print '1.1.1 Backslashes'
  11.  
  12. # Backslash means line continuation:
  13. x = 1 \
  14. + 1
  15. if x <> 2: raise TestFailed, 'backslash for line continuation'
  16.  
  17. # Backslash does not means continuation in comments :\
  18. x = 0
  19. if x <> 0: raise TestFailed, 'backslash ending comment'
  20.  
  21. print '1.1.2 Numeric literals'
  22.  
  23. print '1.1.2.1 Plain integers'
  24. if 0xff <> 255: raise TestFailed, 'hex int'
  25. if 0377 <> 255: raise TestFailed, 'octal int'
  26. if  2147483647   != 017777777777: raise TestFailed, 'large positive int'
  27. try:
  28.     from sys import maxint
  29. except ImportError:
  30.     maxint = 2147483647
  31. if maxint == 2147483647:
  32.     if -2147483647-1 != 020000000000: raise TestFailed, 'max negative int'
  33.     # XXX -2147483648
  34.     if 037777777777 != -1: raise TestFailed, 'oct -1'
  35.     if 0xffffffff != -1: raise TestFailed, 'hex -1'
  36.     for s in '2147483648', '040000000000', '0x100000000':
  37.         try:
  38.             x = eval(s)
  39.         except OverflowError:
  40.             continue
  41.         raise TestFailed, \
  42.               'No OverflowError on huge integer literal ' + `s`
  43. elif eval('maxint == 9223372036854775807'):
  44.     if eval('-9223372036854775807-1 != 01000000000000000000000'):
  45.         raise TestFailed, 'max negative int'
  46.     if eval('01777777777777777777777') != -1: raise TestFailed, 'oct -1'
  47.     if eval('0xffffffffffffffff') != -1: raise TestFailed, 'hex -1'
  48.     for s in '9223372036854775808', '02000000000000000000000', \
  49.          '0x10000000000000000':
  50.         try:
  51.             x = eval(s)
  52.         except OverflowError:
  53.             continue
  54.         raise TestFailed, \
  55.               'No OverflowError on huge integer literal ' + `s`
  56. else:
  57.     print 'Weird maxint value', maxint
  58.  
  59. print '1.1.2.2 Long integers'
  60. x = 0L
  61. x = 0l
  62. x = 0xffffffffffffffffL
  63. x = 0xffffffffffffffffl
  64. x = 077777777777777777L
  65. x = 077777777777777777l
  66. x = 123456789012345678901234567890L
  67. x = 123456789012345678901234567890l
  68.  
  69. print '1.1.2.3 Floating point'
  70. x = 3.14
  71. x = 314.
  72. x = 0.314
  73. # XXX x = 000.314
  74. x = .314
  75. x = 3e14
  76. x = 3E14
  77. x = 3e-14
  78. x = 3e+14
  79. x = 3.e14
  80. x = .3e14
  81. x = 3.1e4
  82.  
  83. print '1.1.3 String literals'
  84.  
  85. def assert(s):
  86.     if not s: raise TestFailed, 'see traceback'
  87.  
  88. x = ''; y = ""; assert(len(x) == 0 and x == y)
  89. x = '\''; y = "'"; assert(len(x) == 1 and x == y and ord(x) == 39)
  90. x = '"'; y = "\""; assert(len(x) == 1 and x == y and ord(x) == 34)
  91. x = "doesn't \"shrink\" does it"
  92. y = 'doesn\'t "shrink" does it'
  93. assert(len(x) == 24 and x == y)
  94. x = "doesn \"shrink\" doesn't it"
  95. y = 'doesn "shrink" doesn\'t it'
  96. assert(len(x) == 25 and x == y)
  97.  
  98.  
  99. print '1.2 Grammar'
  100.  
  101. print 'single_input' # NEWLINE | simple_stmt | compound_stmt NEWLINE
  102. # XXX can't test in a script -- this rule is only used when interactive
  103.  
  104. print 'file_input' # (NEWLINE | stmt)* ENDMARKER
  105. # Being tested as this very moment this very module
  106.  
  107. print 'expr_input' # testlist NEWLINE
  108. # XXX Hard to test -- used only in calls to input()
  109.  
  110. print 'eval_input' # testlist ENDMARKER
  111. x = eval('1, 0 or 1')
  112.  
  113. print 'funcdef'
  114. ### 'def' NAME parameters ':' suite
  115. ### parameters: '(' [varargslist] ')'
  116. ### varargslist: (fpdef ',')* '*' NAME | fpdef (',' fpdef)* [',']
  117. ### fpdef: NAME | '(' fplist ')'
  118. ### fplist: fpdef (',' fpdef)* [',']
  119. def f1(): pass
  120. def f2(one_argument): pass
  121. def f3(two, arguments): pass
  122. def f4(two, (compound, (argument, list))): pass
  123. def a1(one_arg,): pass
  124. def a2(two, args,): pass
  125. def v0(*rest): pass
  126. def v1(a, *rest): pass
  127. def v2(a, b, *rest): pass
  128. def v3(a, (b, c), *rest): pass
  129.  
  130. ### stmt: simple_stmt | compound_stmt
  131. # Tested below
  132.  
  133. ### simple_stmt: small_stmt (';' small_stmt)* [';']
  134. print 'simple_stmt'
  135. x = 1; pass; del x
  136.  
  137. ### small_stmt: expr_stmt | print_stmt  | pass_stmt | del_stmt | flow_stmt | import_stmt | global_stmt | access_stmt | exec_stmt
  138. # Tested below
  139.  
  140. print 'expr_stmt' # (exprlist '=')* exprlist
  141. 1
  142. 1, 2, 3
  143. x = 1
  144. x = 1, 2, 3
  145. x = y = z = 1, 2, 3
  146. x, y, z = 1, 2, 3
  147. abc = a, b, c = x, y, z = xyz = 1, 2, (3, 4)
  148. # NB these variables are deleted below
  149.  
  150. print 'print_stmt' # 'print' (test ',')* [test]
  151. print 1, 2, 3
  152. print 1, 2, 3,
  153. print
  154. print 0 or 1, 0 or 1,
  155. print 0 or 1
  156.  
  157. print 'del_stmt' # 'del' exprlist
  158. del abc
  159. del x, y, (z, xyz)
  160.  
  161. print 'pass_stmt' # 'pass'
  162. pass
  163.  
  164. print 'flow_stmt' # break_stmt | continue_stmt | return_stmt | raise_stmt
  165. # Tested below
  166.  
  167. print 'break_stmt' # 'break'
  168. while 1: break
  169.  
  170. print 'continue_stmt' # 'continue'
  171. i = 1
  172. while i: i = 0; continue
  173.  
  174. print 'return_stmt' # 'return' [testlist]
  175. def g1(): return
  176. def g2(): return 1
  177. g1()
  178. x = g2()
  179.  
  180. print 'raise_stmt' # 'raise' test [',' test]
  181. try: raise RuntimeError, 'just testing'
  182. except RuntimeError: pass
  183. try: raise KeyboardInterrupt
  184. except KeyboardInterrupt: pass
  185.  
  186. print 'import_stmt' # 'import' NAME (',' NAME)* | 'from' NAME 'import' ('*' | NAME (',' NAME)*)
  187. [1]
  188. import sys
  189. [2]
  190. import time, math
  191. [3]
  192. from time import time
  193. [4]
  194. from sys import *
  195. [5]
  196. from math import sin, cos
  197. [6]
  198.  
  199. print 'global_stmt' # 'global' NAME (',' NAME)*
  200. def f():
  201.     global a
  202.     global a, b
  203.     global one, two, three, four, five, six, seven, eight, nine, ten
  204.  
  205. print 'exec_stmt' # 'exec' expr ['in' expr [',' expr]]
  206. def f():
  207.     z = None
  208.     del z
  209.     exec 'z=1+1\n'
  210.     if z <> 2: raise TestFailed, 'exec \'z=1+1\'\\n'
  211.     del z
  212.     exec 'z=1+1'
  213.     if z <> 2: raise TestFailed, 'exec \'z=1+1\''
  214. f()
  215. g = {}
  216. exec 'z = 1' in g
  217. if g <> {'z': 1}: raise TestFailed, 'exec \'z = 1\' in g'
  218. g = {}
  219. l = {}
  220. exec 'global a; a = 1; b = 2' in g, l
  221. if (g, l) <> ({'a':1}, {'b':2}): raise TestFailed, 'exec ... in g, l'
  222.  
  223.  
  224. ### compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
  225. # Tested below
  226.  
  227. print 'if_stmt' # 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
  228. if 1: pass
  229. if 1: pass
  230. else: pass
  231. if 0: pass
  232. elif 0: pass
  233. if 0: pass
  234. elif 0: pass
  235. elif 0: pass
  236. elif 0: pass
  237. else: pass
  238.  
  239. print 'while_stmt' # 'while' test ':' suite ['else' ':' suite]
  240. while 0: pass
  241. while 0: pass
  242. else: pass
  243.  
  244. print 'for_stmt' # 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite]
  245. [1]
  246. for i in 1, 2, 3: pass
  247. [2]
  248. for i, j, k in (): pass
  249. else: pass
  250. [3]
  251.  
  252. print 'try_stmt' # 'try' ':' suite (except_clause ':' suite)+ | 'try' ':' suite 'finally' ':' suite
  253. ### except_clause: 'except' [expr [',' expr]]
  254. try:
  255.     1/0
  256. except ZeroDivisionError:
  257.     pass
  258. try: 1/0
  259. except EOFError: pass
  260. except TypeError, msg: pass
  261. except RuntimeError, msg: pass
  262. except: pass
  263. try: 1/0
  264. except (EOFError, TypeError, ZeroDivisionError): pass
  265. try: 1/0
  266. except (EOFError, TypeError, ZeroDivisionError), msg: pass
  267. try: pass
  268. finally: pass
  269.  
  270. print 'suite' # simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT
  271. if 1: pass
  272. if 1:
  273.     pass
  274. if 1:
  275.     #
  276.     #
  277.     #
  278.     pass
  279.     pass
  280.     #
  281.     pass
  282.     #
  283.  
  284. print 'test'
  285. ### and_test ('or' and_test)*
  286. ### and_test: not_test ('and' not_test)*
  287. ### not_test: 'not' not_test | comparison
  288. if not 1: pass
  289. if 1 and 1: pass
  290. if 1 or 1: pass
  291. if not not not 1: pass
  292. if not 1 and 1 and 1: pass
  293. if 1 and 1 or 1 and 1 and 1 or not 1 and 1: pass
  294.  
  295. print 'comparison'
  296. ### comparison: expr (comp_op expr)*
  297. ### comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not'
  298. if 1: pass
  299. x = (1 == 1)
  300. if 1 == 1: pass
  301. if 1 != 1: pass
  302. if 1 <> 1: pass
  303. if 1 < 1: pass
  304. if 1 > 1: pass
  305. if 1 <= 1: pass
  306. if 1 >= 1: pass
  307. if 1 is 1: pass
  308. if 1 is not 1: pass
  309. if 1 in (): pass
  310. if 1 not in (): pass
  311. if 1 < 1 > 1 == 1 >= 1 <= 1 <> 1 != 1 in 1 not in 1 is 1 is not 1: pass
  312.  
  313. print 'binary mask ops'
  314. x = 1 & 1
  315. x = 1 ^ 1
  316. x = 1 | 1
  317.  
  318. print 'shift ops'
  319. x = 1 << 1
  320. x = 1 >> 1
  321. x = 1 << 1 >> 1
  322.  
  323. print 'additive ops'
  324. x = 1
  325. x = 1 + 1
  326. x = 1 - 1 - 1
  327. x = 1 - 1 + 1 - 1 + 1
  328.  
  329. print 'multiplicative ops'
  330. x = 1 * 1
  331. x = 1 / 1
  332. x = 1 % 1
  333. x = 1 / 1 * 1 % 1
  334.  
  335. print 'unary ops'
  336. x = +1
  337. x = -1
  338. x = ~1
  339. x = ~1 ^ 1 & 1 | 1 & 1 ^ -1
  340. x = -1*1/1 + 1*1 - ---1*1
  341.  
  342. print 'selectors'
  343. ### trailer: '(' [testlist] ')' | '[' subscript ']' | '.' NAME
  344. ### subscript: expr | [expr] ':' [expr]
  345. f1()
  346. f2(1)
  347. f2(1,)
  348. f3(1, 2)
  349. f3(1, 2,)
  350. f4(1, (2, (3, 4)))
  351. v0()
  352. v0(1)
  353. v0(1,)
  354. v0(1,2)
  355. v0(1,2,3,4,5,6,7,8,9,0)
  356. v1(1)
  357. v1(1,)
  358. v1(1,2)
  359. v1(1,2,3)
  360. v1(1,2,3,4,5,6,7,8,9,0)
  361. v2(1,2)
  362. v2(1,2,3)
  363. v2(1,2,3,4)
  364. v2(1,2,3,4,5,6,7,8,9,0)
  365. v3(1,(2,3))
  366. v3(1,(2,3),4)
  367. v3(1,(2,3),4,5,6,7,8,9,0)
  368. import sys, time
  369. c = sys.path[0]
  370. x = time.time()
  371. x = sys.modules['time'].time()
  372. a = '01234'
  373. c = a[0]
  374. c = a[-1]
  375. s = a[0:5]
  376. s = a[:5]
  377. s = a[0:]
  378. s = a[:]
  379. s = a[-5:]
  380. s = a[:-1]
  381. s = a[-4:-3]
  382.  
  383. print 'atoms'
  384. ### atom: '(' [testlist] ')' | '[' [testlist] ']' | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING
  385. ### dictmaker: test ':' test (',' test ':' test)* [',']
  386.  
  387. x = (1)
  388. x = (1 or 2 or 3)
  389. x = (1 or 2 or 3, 2, 3)
  390.  
  391. x = []
  392. x = [1]
  393. x = [1 or 2 or 3]
  394. x = [1 or 2 or 3, 2, 3]
  395. x = []
  396.  
  397. x = {}
  398. x = {'one': 1}
  399. x = {'one': 1,}
  400. x = {'one' or 'two': 1 or 2}
  401. x = {'one': 1, 'two': 2}
  402. x = {'one': 1, 'two': 2,}
  403. x = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5, 'six': 6}
  404.  
  405. x = `x`
  406. x = `1 or 2 or 3`
  407. x = x
  408. x = 'x'
  409. x = 123
  410.  
  411. ### exprlist: expr (',' expr)* [',']
  412. ### testlist: test (',' test)* [',']
  413. # These have been exercised enough above
  414.  
  415. print 'classdef' # 'class' NAME ['(' testlist ')'] ':' suite
  416. class B: pass
  417. class C1(B): pass
  418. class C2(B): pass
  419. class D(C1, C2, B): pass
  420. class C:
  421.     def meth1(self): pass
  422.     def meth2(self, arg): pass
  423.     def meth3(self, a1, a2): pass
  424.